package com.lt.flashcard.model.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.bson.types.ObjectId;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.lt.flashcard.Utility;

/**
 * Store the message from user to user
 * @author ocsen
 *
 */
@Entity("message")
public class Message {

	@Id
	private ObjectId id;
	
	/**
	 * id: 1 of 2 users; from
	 */
	private Long fromId;
	private String fromName;
	private String fromAvatar;
	
	/**
	 * id: 2 of 2 users; to
	 */
	private Long toId;
	private String toName;
	private String toAvatar;
	
	/**
	 * content
	 */
	private String shortMessage;
	
	/**
	 * created date
	 */
	private Date created = new Date();
	
	/**
	 * latest created
	 */
	private Date newest;
	
	/**
	 * count unread message
	 */
	private int unread = 0;
	
	/**
	 * The avatar to display for a message
	 */
	private String avatar = "";
	
	/**
	 * The owner for this message
	 * Every message thread will create a number of message equal with number of recipient
	 */
	private Long owner;
	/**
	 * The thread id
	 */
	private String thread = "";
	
	/**
	 * list message
	 */
	private List<MessageDetail> messages = new ArrayList<MessageDetail>();

	public ObjectId getId() {
		return id;
	}

	public void setId(ObjectId id) {
		this.id = id;
	}

	public Long getFromId() {
		return fromId;
	}

	public void setFromId(Long fromId) {
		this.fromId = fromId;
	}

	public String getFromName() {
		return fromName;
	}

	public void setFromName(String fromName) {
		this.fromName = fromName;
	}

	public Long getToId() {
		return toId;
	}

	public void setToId(Long toId) {
		this.toId = toId;
	}

	public String getToName() {
		return toName;
	}

	public void setToName(String toName) {
		this.toName = toName;
	}

	public String getShortMessage() {
		return shortMessage;
	}

	public void setShortMessage(String shortMessage) {
		this.shortMessage = shortMessage;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public Date getNewest() {
		return newest;
	}

	public void setNewest(Date newest) {
		this.newest = newest;
	}

	public int getUnread() {
		return unread;
	}

	public void setUnread(int unread) {
		this.unread = unread;
	}

	public List<MessageDetail> getMessages() {
		return messages;
	}

	public void setMessages(List<MessageDetail> messages) {
		this.messages = messages;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getFromAvatar() {
		return fromAvatar;
	}

	public void setFromAvatar(String fromAvatar) {
		this.fromAvatar = fromAvatar;
	}

	public String getToAvatar() {
		return toAvatar;
	}

	public void setToAvatar(String toAvatar) {
		this.toAvatar = toAvatar;
	}
	
	/**
	 * Add a new message to this conversation
	 * @param message
	 * @param from This message is "from"
	 */
	public void addMessage(String message, boolean from) {
		if(Utility.isEmptyString(message))
			return;
		message = StringEscapeUtils.escapeHtml3(message);
		
		MessageDetail detail = new MessageDetail();
		//escape the message to display
		detail.setMessage(message);
		
		if(from) {
			detail.setUseravatar(getFromAvatar());
			detail.setUserid(getFromId());
			detail.setUsername(getFromName());
		}
		else {
			detail.setUseravatar(getToAvatar());
			detail.setUserid(getToId());
			detail.setUsername(getToName());
		}
		
		messages.add(detail);
		if((from && !fromId.equals(owner)) || (!from && !toId.equals(owner)))
			unread += 1;
		shortMessage = message;
		newest = detail.getCreated();
	}

	public Long getOwner() {
		return owner;
	}

	public void setOwner(Long owner) {
		this.owner = owner;
	}

	public String getThread() {
		return thread;
	}

	public void setThread(String thread) {
		this.thread = thread;
	}
	
	/**
	 * Clone current mesage to new message
	 */
	public Message clone() {
		Message msg = new Message();
		msg.setAvatar(getAvatar());
		msg.setCreated(getCreated());
		msg.setFromAvatar(getFromAvatar());
		msg.setFromId(getFromId());
		msg.setFromName(getFromName());
		
		for(MessageDetail d : getMessages()) {
			msg.getMessages().add(d.clone());
		}
		
		msg.setNewest(getNewest());
		msg.setOwner(getOwner());
		msg.setShortMessage(getShortMessage());
		msg.setThread(getThread());
		msg.setToAvatar(getToAvatar());
		msg.setToId(getToId());
		msg.setToName(getToName());
		msg.setUnread(getUnread());
		
		return msg;
	}
}